/**
 * Created by Administrator on 2017/4/20.
 */


var Poker = require('../Poker/Poker');

var PokerPlayer = require('../Poker/player').PokerPlayer;

var Const = require('./const');
var SubCode = require('../subCode');

exports.HFGYZPlayer = PokerPlayer.extend({


    init: function(uid, msg) {
        this._super(uid, msg);
        this._stat._roundCards = [];

    },


    checkNoCheck: function(){
        var room = this.getRoom();
        var cards = this._handCards.getCards();
        if(room.getDealerIndex() == this.getIndex()){
            if(this.checkPairs(cards)){
                return true;
            }
        }
        if(this.checkShunZi(cards)){
            return true;
        }
        if(this.checkBomb(cards)){
            return true;
        }
        return false;
    },
    checkPairs: function(cards){
        if(ExObject.isArray(cards)){
            cards = this.getobj(cards);
        }
        var cardsNum = ExObject.sumValue(cards);

        if(cardsNum < 6){
            return false
        }
        if(cardsNum % 2 == 1){
            return false
        }
        var pointCards = [];
        var ANY = 0;
        var player = this;
        ExObject.eachKeyNum(cards, function (c, n) {
            if (c > 100 && n > 0) {
                if(!pointCards[player.getCost(c)]){
                    pointCards[player.getCost(c)] = 0;
                }
                pointCards[player.getCost(c)] += 1;
            }else if(c < 100 && n > 0){
                ANY++;
            }
        });

        for(var i = 1; i < Poker.COLOR_CARD_NUM; i++){
            if(!pointCards[i]){
                continue;
            }
            for (var j = i; j < (i + (cardsNum / 2)) ; j++){
                if(pointCards[j] == 1){
                    if(ANY){
                        ANY--;
                    }else {
                        return false;
                    }
                }else if(pointCards[j] == 2){

                }else {
                    return false;
                }
            }
            return true;
        }
    },
    getCost: function(card){

        return Const.Cost[Poker.getPoint(card)];

    },

    checkShunZi: function(cards){
        if(ExObject.isArray(cards)){
            cards = this.getobj(cards);
        }
        var cardsNum = ExObject.sumValue(cards);

        if(cardsNum < 3){
            return false
        }
        var player = this;

        var room = this.getRoom();
        var colors = room.getSubConfig().colors;
        var pointCards = [];
        var ANY = 0;
        ExObject.eachKeyNum(cards, function (c, n) {
            if (c > 100 && n > 0) {
                if(!pointCards[player.getCost(c)]){
                    pointCards[player.getCost(c)] = 0;
                }
                pointCards[player.getCost(c)] += 1;
            }else if(c < 100 && n > 0){
                ANY++;
            }
        });

        for(var i = 1; i < Poker.COLOR_CARD_NUM; i++){
            if(!pointCards[i]){
                continue;
            }
            for (var j = i; j < (i + cardsNum); j++){
                if(!pointCards[j]){
                    if(ANY){
                        ANY--;
                    }else {
                        return false;
                    }
                }
            }
            return true;
        }
    },
    checkBomb: function(cards){
        if(ExObject.isArray(cards)){
            cards = this.getobj(cards);
        }
        var cardsNum = ExObject.sumValue(cards);

        if(cardsNum < 3){
            return false
        }
        var player = this;

        var pointCards = [];
        var ANY = 0;
        ExObject.eachKeyNum(cards, function (c, n) {
            if (c > 100 && n > 0) {
                if(!pointCards[Poker.getPoint(c)]){
                    pointCards[Poker.getPoint(c)] = 0;
                }
                pointCards[Poker.getPoint(c)] += 1;
            }else if(c < 100 && n > 0){
                ANY++;
            }
        });

        for(var i = 1; i <= Poker.COLOR_CARD_NUM; i++){
            if(!pointCards[i]){
                continue;
            }
            if(pointCards[i] + ANY == cardsNum){
                return true;
            }else{
                return false;
            }
        }
    },
    checkMaxCard: function(cards){
        if(ExObject.isArray(cards)){
            cards = this.getobj(cards);
        }
        var player = this;
        var cardsNum = ExObject.sumValue(cards);
        var maxCard = 0;
        var minCard = 0;
        var num = 0;
        ExObject.eachKeyNum(cards, function (c, n) {
            if (c > 100 && n > 0) {
                if(player.getCost(c) > maxCard){
                    maxCard = player.getCost(c);
                }
            }else {
                num++;
            }
        });
        if(!num){
            var max = [];
            max[0] = maxCard;
            return max;
        }
        minCard = maxCard;
        ExObject.eachKeyNum(cards, function (c, n) {
            if (c > 100 && n > 0) {
                if(player.getCost(c) < minCard){
                    minCard = player.getCost(c);
                }
            }
        });
        var iTmp = maxCard - minCard;
        if(iTmp == 0){
            var max = [];
            max[0] = maxCard;
            return max;
        }
        if(iTmp == cardsNum - 1){
            var max = [];
            max[0] = maxCard;
            return max;
        }else {
            var more = cardsNum - 1 - iTmp;
            var max = [];
            for (var i = 0; i <= more; i++){
                if(maxCard + i > 12){
                    break;
                }
                max[i] = maxCard + i;
                if(max[i] - cardsNum < 0){
                    max[i] += 1;
                }
            }
            return max
        }
    },
    playCards:function(cards){
        if(ExObject.isArray(cards)){
            cards = this.getobj(cards);
        }
        var player = this;
        ExObject.eachKeyNum(cards, function (c, n) {
            player._handCards.delCard(c);
        });
    },
    getobj: function(cards){
        var obj = {};
        for(var i = 0; i < cards.length; i++){
            ExObject.addNumber0(obj,cards[i],1)
        }
        return obj;
    },
    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getInfo: function() {
        var room = this.getRoom();
        var info = this._super();

        info.handCards = room._settlementStatus ? {} : this._handCards.getCards();

        info.score = this._stat.score;
        info.roundCards = this._stat._roundCards;
        info.remainScore = this._stat.remainScore;
        return info;
    },

    getReconnectInfo: function(isSelf) {
        var info = this._super(isSelf);
        info.handCards = this._handCards.getCards();
        info.remainScore = this._stat.score;
        info.roundCards = this._stat._roundCards;
        return info;
    },

    getSettlementInfo: function() {
        var info = this._super();
        info.handCards = this._handCards.getCards();
        return info;
    },

    settlementPattern: function() {
        var room = this.getRoom();
        var player = this;
        if(this.getIndex() != room.RoundWinPlayer){
            return;
        }
        var score = 0

        room.enumPlayers(function(eIndex, ePlayer){
            if(eIndex == player.getIndex){
                return;
            }
            if(ePlayer._handCards.getCardNum() >= 5){
                score = 10;
            }else{
                score = ePlayer._handCards.getCardNum();
            }
            player.addRoundScore(score);
            ePlayer.addRoundScore(-score);

        })

    },

    settlement: function() {
        this._super();

    },
    getInitHandCardNeedNum: function() {
        var room = this.getRoom();
        if(this.getIndex() == room.getDealerIndex()) {
            return 6;
        } else {
            return 5;
        }
    },
    onStartNewRound: function() {
        this._super();
        this._stat._roundCards = [];

        this._stat.roundPattern = Const.Pattern.NONE;
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/


    _getMaxCard: function() {
        var maxCard = 0;

        var allCardsArr = this._handCards.getInitCards();

        allCardsArr.forEach(function(card) {

        });

        return maxCard;
    },
    getInitHandCardPreCards: function() {
        return this._super();
    },
});